﻿using CPPEI.Coolzon.Metadata;
using CPPEI.Coolzon.Theme.Fashion.MainViewControl;
using CPPEI.Coolzon.UI;
using CPPEI.Coolzon.UI.Event;
using CPPEI.Coolzon.UI.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CPPEI.Coolzon.Event.NCL;
using CPPEI.Coolzon.Event.UI;
using CPPEI.Coolzon.UI.Data;
using CPPEI.Coolzon.Event.Core;

namespace CPPEI.Coolzon.Theme.Fashion
{
    /// <summary>
    /// MainView.xaml 的交互逻辑
    /// </summary>
    public partial class MainView : CPPEI.Coolzon.UI.View, IMainView
    {
        SelectSceneView _viewSelectScene;

        private Scene _scene;
        public MainView()
        {
            InitializeComponent();
            ExtentionFunctionLoader.Instance.Load(string.Format("{0}/config/extentionfunction.xml", AppDomain.CurrentDomain.BaseDirectory));
            
        }
        protected override void OnActive()
        {
            base.OnActive();

            CPPEI.Coolzon.UI.Views.RenderView renderView = (CPPEI.Coolzon.UI.Views.RenderView)UI.UIContext.Current.RenderView;
            sceneContent.Child = renderView;


            _viewSelectScene = new SelectSceneView();
            Common.EventBus.Instance.Register(_viewSelectScene);
            CoreEventBus.Register(_viewSelectScene);

            // 将事件远程处理模式的代理实例注册到事件总线上
            RemoteEventProxy.Instance.SetRenderView(renderView);

            Common.EventBus.Instance.Register(header);
            Common.EventBus.Instance.Register(sceneContent);
            Common.EventBus.Instance.Register(operationNavigation);
            Common.EventBus.Instance.Register(jsControl);
            Common.EventBus.Instance.Register(zoomView);
            Common.EventBus.Instance.Register(this);

            NCLEventBus.Register(header);
            NCLEventBus.Register(sceneContent);
            NCLEventBus.Register(operationNavigation);
            NCLEventBus.Register(jsControl);
            NCLEventBus.Register(zoomView);
            NCLEventBus.Register(this);

            UIEventBus.Register(header);
            UIEventBus.Register(sceneContent);
            UIEventBus.Register(operationNavigation);
            UIEventBus.Register(jsControl);
            UIEventBus.Register(zoomView);
            UIEventBus.Register(this);

            CoreEventBus.Register(header);
            CoreEventBus.Register(sceneContent);
            CoreEventBus.Register(operationNavigation);
            CoreEventBus.Register(jsControl);
            CoreEventBus.Register(zoomView);
            CoreEventBus.Register(this);

            Common.EventBus.Create(ServiceProxy.ClientProxyManager.EVENTBUS_FLAGNAME).Register(renderView);

            OpenDefaultSceneThread.Start(UIContext.Current.Solution);
        }

        protected override void OnDeactive()
        {
            // 清空场景选择界面上的按钮,因为按钮上关联了场景Metadata
            _viewSelectScene.Clear();
            Common.EventBus.Instance.Unregister(_viewSelectScene);
            CoreEventBus.Unregister(_viewSelectScene);

            this.zoomView.Close();

            CPPEI.Coolzon.UI.Views.RenderView renderView = (CPPEI.Coolzon.UI.Views.RenderView)UI.UIContext.Current.RenderView;
            sceneContent.Child = null;
            // 从事件总线撤销注册
            Common.EventBus.Instance.Unregister(renderView);
            Common.EventBus.Instance.Unregister(header);
            Common.EventBus.Instance.Unregister(sceneContent);
            Common.EventBus.Instance.Unregister(operationNavigation);
            Common.EventBus.Instance.Unregister(jsControl);
            Common.EventBus.Instance.Unregister(zoomView);
            Common.EventBus.Instance.Unregister(this);

            NCLEventBus.Unregister(renderView);
            NCLEventBus.Unregister(header);
            NCLEventBus.Unregister(sceneContent);
            NCLEventBus.Unregister(operationNavigation);
            NCLEventBus.Unregister(jsControl);
            NCLEventBus.Unregister(zoomView);
            NCLEventBus.Unregister(this);

            NCLEventBus.Unregister(renderView);
            UIEventBus.Unregister(header);
            UIEventBus.Unregister(sceneContent);
            UIEventBus.Unregister(operationNavigation);
            UIEventBus.Unregister(jsControl);
            UIEventBus.Unregister(zoomView);
            UIEventBus.Unregister(this);

            CoreEventBus.Unregister(header);
            CoreEventBus.Unregister(sceneContent);
            CoreEventBus.Unregister(operationNavigation);
            CoreEventBus.Unregister(jsControl);
            CoreEventBus.Unregister(zoomView);
            CoreEventBus.Unregister(this);

            Common.EventBus.Create(ServiceProxy.ClientProxyManager.EVENTBUS_FLAGNAME).Unregister(renderView);

            base.OnDeactive();
        }



        private void closeImg_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Common.EventBus.Instance.Publish(new CloseNavEvent());
        }

        private void OnBtnCloseMagnifierClick(object sender, RoutedEventArgs e)
        {
            UIEventBus.Publish(new UIShowMagnifierEvent(false));
        }
        private void OnBtnCloseLaserPointClick(object sender, RoutedEventArgs e)
        {
            UIEventBus.Publish(new UIShowLaserPointEvent(false));
        }
    }
}
